package com.example.ai.model.viewmodel

import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider

/**
 * Created by lijinxi on 2024/3/7.
 * @Description:
 */
class ViewModelFactory<T : ViewModel>(
    private val viewModelSupplier: () -> T
) : ViewModelProvider.Factory {

    @Suppress("UNCHECKED_CAST")
    override fun <T : ViewModel> create(modelClass: Class<T>): T {
        if (modelClass.isAssignableFrom(viewModelSupplier().javaClass)) {
            return viewModelSupplier() as T
        }
        throw IllegalArgumentException("Unknown ViewModel class")
    }
}